<?php
namespace App\Services\Office;

use Excel;


class RoundOffice
{
    //pdf主体
    public function pdfBody($data,$full_name,$expired_time,$parsed_data='',$online_time){
        $online_time = explode("-", $online_time);
        $sp_time = explode("-", $expired_time);
        $company = $data[0]['company']['company_name'];
        $pdf = new \TCPDF();
        $pdf->setPrintHeader(false);
        $pdf->setPrintFooter(false);

        // 设置默认等宽字体
        $pdf->SetDefaultMonospacedFont('courier');
        $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
        $pdf->setFontSubsetting(true);

        //设置字体 stsongstdlight支持中文
        $pdf->SetFont('stsongstdlight', '', 14);

        //第一页
        $pdf->AddPage();
        $html = <<<EOD
<h1 style="text-align:center">授权书</h1>
<p style="font-weight:bold">授权方：$company</p>
<p style="font-weight:bold">被授权方：$full_name</p>
<p></p>
<p>基于双方于2017年02月20日签署的编号为 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 的《音乐作品合作协议》，</p>
EOD;
        $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);
        $pdf->writeHTML('明确授权音乐作品如下：', true, false, true, false, '');
        $table="
<table border=\"1\" align=\"center\">
<tr>
<td>序号</td>
<td>作品/制品名称</td>
<td>表演者</td>
<td>词作者</td>
<td>曲作者</td>
<td>表演者、录音制作者邻接权</td>
<td>视频（MV）权利</td>
<td>词著作权</td>
<td>曲著作权</td>

</tr>";
        foreach($data as $i=>$v){
            $table.="
<tr>
<td>".($i+1)."</td>
<td>".$v['title']."</td>
<td>".$v['performer']."</td>
<td>".$v['writer']."</td>
<td>".$v['composer']."</td>
<td>".$v['neighboring_right_1']."%</td>
<td>100%</td>
<td>".$v['neighboring_right_2']."%</td>
<td>".$v['neighboring_right_3']."%</td>
</tr>";
        }
        $table.="</table>";
        $pdf->writeHTML($table, true, false, false, false, '');
        $html2 = <<<EOD
        <p>授权期限：自$online_time[0]年$online_time[1]月$online_time[2]日至$sp_time[0]年$sp_time[1]月$sp_time[2]日</p>
        <p style="text-align: right">授权方：$company</p>
        <p style="text-align: right;margin-right:30px">（签字/公章）</p>
        <p></p>
        <p style="text-align: right">签署日期： $online_time[0]年$online_time[1]月$online_time[2]日</p>
EOD;
        $pdf->writeHTMLCell(0, 0, '', '', $html2, 0, 1, 0, true, '', true);
        if($parsed_data!='') {
            $pdf->Image(public_path($parsed_data['path']), '145', $pdf->GetY()-35, 40, 40, '', '', '', false, 300, '', false, false, 1, false, false, false);
        }
        return $pdf;
    }

    //生成盖章pdf
    public function makeSignPdf($data,$full_name,$simple_name,$expired_time,$rand,$image,$fix_time,$sp_email,$online_time)
    {
        $parsed_data = parse_url($image);
        $pdf = $this->pdfBody($data,$full_name,$expired_time,$parsed_data,$online_time);
        $num = $pdf->getNumPages();
        $path = substr($parsed_data['path'],0,strrpos($parsed_data['path'],'/')+1);
        $name = substr($parsed_data['path'],strrpos($parsed_data['path'],'/')+1);
        $fname = $_SERVER['DOCUMENT_ROOT'].$path.'/small_'.$name;
        if(file_exists($fname)){
            $toImage = $fname;
        }else{
            $toImage = $_SERVER['DOCUMENT_ROOT'].$parsed_data['path'];
        }
        $ext = explode(".", $image);
        $ext = $ext[count($ext)-1];
        if($num>1) {
            cutImage($toImage, $num, $ext);
            for ($i = 0; $i < $num; $i++) {
                $pdf->setPage(($i + 1));
                $pdf->Image('image/' . $i . '.' . $ext, '180', '120', 25,  '', '', '', 'T', false, 300, '', false, false, 1, false, false, false);
            }
        }
        //输出PDF
        $data = $pdf->Output('t.pdf', 'S');//I输出、D下载
        $dir = date('Y-m-d').'-sign-'.$rand;
        @mkdir(storage_path('/exports/'.$dir),0777);
        file_put_contents(storage_path('/exports/'.date('Y-m-d').'-sign-'.$rand.'/'.$simple_name . date('Y-m-d').'-sign.pdf'),$data);
        return $dir;
    }

    //生成无章pdf
    public function makePdf($data,$full_name,$simple_name,$expired_time,$rand,$fix_time,$sp_email,$online_time){
        $pdf = $this->pdfBody($data,$full_name,$expired_time,'',$online_time);
        $data = $pdf->Output('t.pdf', 'S');//I输出、D下载
        $dir = date('Y-m-d').'-'.$rand;
        @mkdir(storage_path('/exports/'.$dir),0777);
        file_put_contents(storage_path('/exports/'.date('Y-m-d').'-'.$rand.'/'.$simple_name . date('Y-m-d').'.pdf'),$data);
        return $dir;
    }


}
